﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BeanGo.DataGridDemo.Core
{
    public class TestConfig : Bandit.UI.MVVM.NotifyObject
    {
        private static string _TestConfigFileName = "".GetBaseDirectory() + "TestConfig.xml";

        #region 实例
        private static TestConfig _Instance;
        /// <summary>
        /// 实例
        /// </summary>
        public static TestConfig Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new TestConfig();
                    _Instance.InitTestConfig();
                }
                return _Instance;
            }
            set
            {
                _Instance = value;
            }
        }
        #endregion

        private string _Project;
        /// <summary>
        /// 专案名
        /// </summary>
        public string Project
        {
            get { return _Project; }
            set
            {
                _Project = value;
                OnPropertyChanged("Project");
            }
        }

        private string _Station;
        /// <summary>
        /// 站点名
        /// </summary>
        public string Station
        {
            get { return _Station; }
            set
            {
                _Station = value;
                OnPropertyChanged("Station");
            }
        }

        private bool _IsLoopTest;
        /// <summary>
        /// 是否循环测试
        /// </summary>
        public bool IsLoopTest
        {
            get { return _IsLoopTest; }
            set
            {
                _IsLoopTest = value;
                OnPropertyChanged("IsLoopTest");
            }
        }

        private int _LoopTestInterval;
        /// <summary>
        /// 循环测试间隔(秒)
        /// </summary>
        public int LoopTestInterval
        {
            get { return _LoopTestInterval; }
            set
            {
                _LoopTestInterval = value;
                OnPropertyChanged("LoopTestInterval");
            }
        }



        public TestConfig()
        {

        }

        public void InitTestConfig()
        {
            this.LoopTestInterval = 10;
        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public static void SaveConfig(string path)
        {
            Bandit.Helpers.ConfigHelper.XmlFileSerialize<TestConfig>(TestConfig.Instance, path);
        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        public static void SaveConfig()
        {
            SaveConfig(TestConfig._TestConfigFileName);
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public static void LoadConfig(string path)
        {
            if (System.IO.File.Exists(path))
            {
                TestConfig._Instance = Bandit.Helpers.ConfigHelper.XmlFileDeserialize<TestConfig>(path);

                //if (TestConfig._Instance.Product.ColumnCount == 0)
                //{
                //    TestConfig._Instance.Product.ColumnCount = 8;
                //}
                //else
                //{
                //    foreach (var item in TestConfig._Instance.Product.Pins)
                //    {
                //        item.Name = item.Limit.Name;
                //    }
                //}

                if (path != TestConfig._TestConfigFileName)
                {
                    SaveConfig();
                }
            }
            else
            {
                if (path == TestConfig._TestConfigFileName)
                {
                    TestConfig.Instance = new TestConfig();
                    TestConfig.Instance.InitTestConfig();
                }
            }
        }

        /// <summary>
        /// Loads the config.
        /// </summary>
        public static void LoadConfig()
        {
            LoadConfig(TestConfig._TestConfigFileName);
        }

        public override string ToString()
        {
            return string.Format("{0}-{1}", this.Project, this.Station);
        }
    }
}
